<?php
class ml_model_dbContent extends Lib_datamodel_db
{
    private $status_del  = 9;
    private $audit_status_del = 9;
    private $status_ok      = 1;
    private $tb_n;
    
    public function __construct() {
        
        $db_config = ml_factory::load_standard_conf ( 'dbContent' );
        $this->tb_n = $db_config['content']['tb_n'];
        parent::__construct ( 'content', $db_config ['content'] );
    }

    protected function hook_after_fetch()
    {
        $data = $this->get_data();

        if(isset($data['gd_info']))
        {
            $data['gd_info'] = unserialize($data['gd_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['gd_info']))
                {
                    $val['gd_info'] =unserialize($val['gd_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }

        return true;
    }
    protected function hook_before_write($array)
    {
        if(isset($array['gd_info']))
        {
            $array['gd_info'] = serialize($array['gd_info']);
        }
        return $array;
    }




    public function get_by_rid($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        $sql = 'select * from `'.$this->table.'` where `rid`='.'\''.$rid.'\'  AND  `status`!='.$this->status_del;
        $rs=$this->fetch_row($sql);
        return $rs?$this->_data:false;
    }
    
    
    public function get_cate_by_rid($rid){
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        $sql = 'select `gd_catelog` from `'.$this->table.'` where `rid`='.'\''.$rid.'\'';
        $rs=$this->fetch_row($sql);
        return $rs?$this->_data:false;
    }
    /**
     *
     * 输入rid数组，输出内容
     * xinhua
     * @param unknown_type $rid数组
     *
     */
    public function get_content_by_rids($aRids,$is_guang=false)
    {
        if(empty($aRids)){
            return null;                                        //参数错误
        }
        
        $rid_uid = array();
        foreach ($aRids as $rid)
        {
            $t_uid  = ml_tool_resid::resid2uid($rid);
            $suffix = Tool_sina::calc_hash_tbl($t_uid, $this->tb_n);
            $aUid2Rid[$suffix][] = $rid;
            $rid_uid[$suffix] = $t_uid;
        }

        $data=array();                                        //内容数据
        $guang_where = '';
        if($is_guang){
            $guang_where = ' and `audit_status`!='.$this->audit_status_del;
        }
        foreach ($aUid2Rid as $suffix => $rids) {

            $this->init_db($rid_uid[$suffix],self::DB_SLAVE);
            $sRid = '"'.implode('","' , $rids).'"';
            $sql = 'select * from `'.$this->table.'` where  `rid` in ('.$sRid.') and `status`='.$this->status_ok.$guang_where.' order by `ctime` desc';
            $rs = $this->fetch($sql);
            $data[]=$rs? $this->_data: null;

        }
        $arr=array();
        
        foreach ($data as $value) {
            if($value)
            $arr=array_merge($arr,$value);
        }
        //$arr  = Tool_array::array_sort($arr, 'ctime','desc');
        
        $data = Tool_array::format_2d_array($arr , 'rid' , Tool_array::FORMAT_FIELD2ROW);
        $final=array();
        foreach ($aRids as $v) {
            if($data[$v])
            $final[$v]=$data[$v];
        }
        
        return $final;
    }
    /**
     *
     * 输入uid，输出内容
     * xinhua
     * @param unknown_type $uid
     *@param unknown_type $begin
     *@param unknown_type $count
     */
    public function get_content_by_uid($uid,$begin,$count,$fieldArr=array()){

        if(empty($uid)){
            return null;                                        //参数错误
        }
        
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        $fields='';
        if(!is_array($fieldArr) || count($fieldArr)==0 ){
            $fields='  *  ';
        }else{
            $fields='`'.implode('` , `',$fieldArr).'`'; 
        }
        
        $sql = "select  ".$fields."  from  `$this->table`  where `uid`='$uid' and status!='$this->status_del'  ORDER BY ctime DESC  limit $begin,$count";
        
        $rs = $this->fetch($sql);
       
        return  $rs? $this->_data: null;

    }
    /**
     *
     * 输入uid，输出总数
     * xinhua
     * @param unknown_type $uid
     *
     */
    public function get_count_by_uid($uid){

        if(empty($uid)){
            return null;                                        //参数错误
        }
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;

        $sql = "select count(*) as `num` from `$this->table` where `uid`='$uid' and status!='$this->status_del'";
        $rs = $this->fetch($sql);
        
        return  $rs? $this->_data[0]: null;
            
    }

    public function update_catelog_by_rid($rid , $catelog)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
            
        $aUpdate = array(
            'gd_catelog' => $catelog
        );
        return $this->update($aUpdate , '`rid`="'.$this->escape($rid).'"' , $this->status_ok);
    }
    
    public function update_after_analyse_by_rid($rid , $catelog , $aTag)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
            return false;
            
        $this->get_by_rid($rid);
        $gd_info = $this->_data['gd_info'];
        $gd_info['tags'] = $aTag;
            
        $aUpdate = array(
            'gd_catelog' => $catelog,
            'gd_info' => $gd_info
        );
        return $this->update($aUpdate , '`rid`="'.$this->escape($rid).'"' , $this->status_ok);
    }

    // 修改gd_info
    public function update_gd_info($rid,$gd_info){
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
            return false;
        
        $aUpdate = array(
            'gd_info' => $gd_info
        );
        return $this->update($aUpdate , '`rid`="'.$this->escape($rid).'"' , $this->status_ok);    
    }
    public function update_content($rid,$content){
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
            return false;
    
        $aUpdate = array(
                'content' => $content
        );
        return $this->update($aUpdate , '`rid`="'.$this->escape($rid).'"' , $this->status_ok);
    }
    
    public function insert_content($data){
        $uid = $data['uid'];
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
        $array['rid'] = $data['rid'];
        $array['uid'] = $data['uid'];
        $array['type_xrank'] = $data['type_xrank'];
        $array['content'] = $data['content'];
        $array['pic_id'] = $data['pic_id'];
        $array['gd_title'] = $data['gd_title'];
        $array['gd_price'] = $data['gd_price'];
        $array['gd_info'] = $data['gd_info'];
        $array['from'] = empty($data['from']) ? ML_FROM_WEB : $data['from'];
        $array['userfeed_rid'] = $data['userfeed_rid'];
        $array['status'] = $this->status_ok;
        $array['ctime'] = $data['ctime'];
        $array['cip'] = $data['cip'];
        return $this->insert($array);

    }

    public function del_contentByRid($rid){
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
            

        return $this->update(array('status' => $this->status_del), '`rid` = "'.$rid.'"', $this->status_ok);
    }

    public function del_contentByUserfeed_rid($userfeed_rid){
        $uid = ml_tool_resid::resid2uid($userfeed_rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
        return $this->update(array('status' => $this->status_del), '`userfeed_rid` = "'.$userfeed_rid.'"', $this->status_ok);

    }
    
    /**
     * sitemap 用 按时间取最新的RID
     *
     * @param string $table
     * @param date $ctime
     * @return bool
     */
    public function get_rid_by_sitemap($table,$ctime=''){
        if(!$table)
        return false;

        $this->init_db();
        if(empty($ctime)){
            $sql = "select `rid` from  ml_content_".$table;
        }
        else{
            $sql = "select `rid` from  ml_content_".$table." where `ctime`>'$ctime'  AND  `status`!=$this->status_del";    
        }

        return $this->fetch($sql);
    }
}
?>